package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.exception.BusinessException;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author LiLi
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api("后台管理积分等级接口")
@Slf4j
//@CrossOrigin
public class AdminIntegralGradeController {
    @Autowired
    IntegralGradeService integralGradeService;

    @GetMapping("list")
    @ApiOperation("积分等级列表")
    public R list(){

//        log.info("LiLi");
//        log.warn("warning !!!");
//        log.error("it's a error");


        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list);
    }



    @DeleteMapping("remove/{id}")
    @ApiOperation("删除积分等级")
    public R remove(
            @ApiParam("删除的主键")
            @PathVariable("id") Long id){
        integralGradeService.removeById(id);
        return R.ok().message("删除成功");
    }
    @PostMapping("save")
    @ApiOperation("新增积分等级")
    public R save(@ApiParam("积分等级对象")
                  @RequestBody
                  IntegralGrade integralGrade) //throws Exception
    {
//        log.info("LiLi");
//        log.warn("warning !!!");
//        log.error("it's a error");

        BigDecimal borrowAmount = integralGrade.getBorrowAmount();
        int i = borrowAmount.compareTo(new BigDecimal("0"));//-1 0 1
//        if(i!=1){
//            //借款额度不能小于等于0
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_0_ERROR);
//        }

        Assert.isTrue(i==1,ResponseEnum.BORROW_AMOUNT_0_ERROR);
        //策略判断 assert
        if(null!=integralGrade.getId()&&integralGrade.getId()>0){
            integralGradeService.updateById(integralGrade);
        }else {
            integralGradeService.save(integralGrade);
        }

        return R.ok();
    }

    @GetMapping("get/{id}")
    @ApiOperation("根据id查询积分等级")
    public R get(
        @ApiParam("回显的主键")
        @PathVariable("id") Long id){
        IntegralGrade byId = integralGradeService.getById(id);
        return R.ok().data("record",byId);
        }
}

